package com.the_last.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.the_last.pojo.dto.UserDTO;
import com.the_last.pojo.po.User;
import com.the_last.pojo.vo.UserVO;
import com.the_last.service.IUserService;
import com.the_last.utils.Result;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    
    private final IUserService userService;
    
    /**
     * 分页查询用户列表
     */
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result<Page<UserVO>> getUserList(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "10") Integer size,
            UserDTO userDTO) {
        
        Page<User> page = new Page<>(current, size);
        return userService.getUserList(page, userDTO);
    }
    
    /**
     * 根据ID获取用户详情
     */
    @GetMapping("/{id}")
    @PreAuthorize("hasAuthority('USER_QUERY')")
    public Result<UserVO> getUserById(@PathVariable Long id) {
        return userService.getUserById(id);
    }
    
    /**
     * 新增用户
     */
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('USER_CREATE')")
    public Result<Void> addUser(@Valid @RequestBody UserDTO userDTO) {
        return userService.addUser(userDTO);
    }
    
    /**
     * 更新用户信息
     */
    @PutMapping("/update")
    @PreAuthorize("hasAuthority('USER_UPDATE')")
    public Result<Void> updateUser(@Valid @RequestBody UserDTO userDTO) {
        return userService.updateUser(userDTO);
    }
    
    /**
     * 删除用户
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('USER_DELETE')")
    public Result<Void> deleteUser(@PathVariable Long id) {
        return userService.deleteUser(id);
    }
    
    /**
     * 批量更新用户状态
     */
    @PutMapping("/update-status")
    @PreAuthorize("hasAuthority('USER_UPDATE')")
    public Result<Void> updateUserStatus(@RequestParam List<Long> userIds, @RequestParam Integer status) {
        return userService.updateUserStatus(userIds, status);
    }
    
    /**
     * 重置用户密码
     */
    @PutMapping("/reset-password")
    @PreAuthorize("hasAuthority('USER_UPDATE')")
    public Result<Void> resetPassword(@RequestParam Long userId, @RequestParam String newPassword) {
        return userService.resetPassword(userId, newPassword);
    }
    
}